clear all	
cd "${data}"	
use "cses5.dta", clear 

rename E3001 interest

gen daysafter = E1035_1
replace daysafter = . if daysafter==9995 | daysafter==9999

rename E1005 id


rename E3019_*? E3019_#, renumber	// voter perception
recode E3019_* (95/99=.)
rename E5018_*? E5018_#, renumber	// expert perception 
recode E5018_* (99=.)


rename E1006_NAM country // country
rename E1004 election // election

rename E5000_*? E5000_#, renumber	// party identifier

keep interest daysafter E3019_* E5018_* country election


keep if country=="Australia" | country=="Austria" | country=="Belgium" | country=="Canada" | country=="Denmark" | country=="Finland" | country=="France" | country=="Germany" | country=="Greece" | country=="Iceland" | country=="Ireland" | country=="Italy" | country=="Netherlands" | country=="New Zealand" | country=="Norway" | country=="Portugal" | country=="Spain" | country=="Sweden" | country=="Switzerland" | country=="Great Britain" | country=="United States of America" 

gen respondent = _n

encode country, generate(country_)
encode election, generate(election_)

  
  gen expressedinterest = . 
  replace expressedinterest = 0 if interest == 4
  replace expressedinterest = 1 if interest == 3
  replace expressedinterest = 2 if interest == 2
  replace expressedinterest = 3 if interest == 1
 

eststo m1_expressedinterest: regress expressedinterest daysafter i.election_, robust 
 margins, at(daysafter=(0(30)300))
marginsplot, ///
    ytitle("predicted expressed political interest", size(3.5)) ///
    recastci(rarea) level(95) ///
    recast(line) ///
    plot1opts(lcolor(gs12) mcolor(gs12) lpattern(shortdash)) ///
    title("") ///
    addplot(hist daysafter, percent color(gs12%50) yaxis(2) ///
        ytitle("percent", size(3.5) axis(2)) ///
        yscale(range(0(5)25) axis(2) alt)) ///
    legend(off) ///
    xscale(range(0(30)300)) ///
    xlabel(0(30)300) ///
    ciopt(color(gs12))

cd "${figures}"	
graph save "figureA1.gph", replace
graph export "figureA1.png", replace 


reshape long E3019_ E5018_, i(respondent) j(partyorder)

drop if daysafter==.
drop if interest==.
drop if E3019_==.
drop if E5018_==.

gen expressedinterest_ = .
replace expressedinterest_ = 0 if expressedinterest == 0 | expressedinterest == 1
replace expressedinterest_ = 1 if expressedinterest == 2 | expressedinterest == 3

label define interestlabels 0 "low political interest" 1 "high political interest"
label values expressedinterest_ interestlabels
 

eststo m2_expressedinterest: mixed c.E3019_ c.daysafter##c.E5018_##i.expressedinterest_ || election_: expressedinterest_ || respondent: E5018_ 
margins, dydx(E5018_) at(daysafter=(0(30)300)) over(expressedinterest_)

marginsplot, ///
    ytitle("AME of a one-unit change in expert party position", size(3.5)) ///
    recastci(rarea) ciopt(color(gs12%50)) level(95) ///
    recast(line) ///
    plot1opts(color(gs12%50) lpattern(shortdash)) ///
    plot2opts(color(gs5%50) lpattern(solid)) ///
    title("") ///
    addplot(hist daysafter, percent color(gs12%50) yaxis(2) ///
        ytitle("percent", size(3) axis(2)) ///
        yscale(range(0(5)25) axis(2) alt)) ///
    xscale(range(0(30)300)) ///
    xlabel(0(30)300) ///
    plotr(lp(solid) lcolor(black)) ///
    xtitle("days since election", size(3.5)) ///
    legend( ///
        position(6) ring(1) rows(1) ///
        order(1 "low political interest" 2 "high political interest") ///
        size(small) region(lstyle(none)) )
cd "${figures}"	
graph save "figureA2.gph", replace
graph export "figureA2.png", replace 
   

cd "${tables}"	

  esttab m1_expressedinterest m2_expressedinterest using tableA1.tex ,replace  b(3) se(3) nogap transform(ln*: exp(2*@) 2*exp(2*@)) label mlabels ("Model 1" "Model 2")

  